package cn.com.cookie;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "addCookieServlet", value = "/addCookieServlet")
public class addCookieServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //创建Cookie对象
        Cookie cookie = new Cookie("mycookie","第一个cookie");
        //设置cookie在浏览器中保持1小时
        cookie.setMaxAge(60*60);
        //加入到浏览器的cookies集合中
        response.addCookie(cookie);
        response.getWriter().write(cookie.getName() + ":" + cookie.getValue());
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
